package com.sline.servlet.demo1_servlet._03_forward_redirect;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/forward/a")
public class aServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html; charset-utf-8");

        PrintWriter writer = resp.getWriter();

        String name = req.getParameter("name");
        System.out.println("aServlet ----- before ----- name = " + name);
        writer.println("aServlet ----- before");

        /*  forward(请求转发)	        redirect(URL重定向)
            请求地址不变	            请求地址改变
            请求参数可以传递	        请求参数不能传递
            只有一个请求	            有多个请求
            可以访问WEB-INF中的资源	不可以访问WEB-INF中的资源
            请求转发不能跨域	        请求转发可以跨域
            最终响应由bServlet决定 	最终响应由bServlet决定
        */
//        RequestDispatcher requestDispatcher = req.getRequestDispatcher("/forward/b");
//        requestDispatcher.forward(req, resp);
        resp.sendRedirect("/servlet/forward/b");

        System.out.println("aServlet ----- after");
        writer.println("aServlet ----- after");
    }
}
